package com.example.mobile2030606174mzq0622;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button BtnLaert;
    private Button BtnApi;
    private Button Btn6days;
    private Button BtnCondition;
    private Button BtnMainLimit;
    private TextView tvMainData;
    OkHttpClient okHttpClient = new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        BtnCondition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        RequestBody requestBody = new FormBody.Builder()
                                .add("cityid","917")
                                .add("token",MojiDataManager.CONDITIONTOKEN)
                                .build();

                        Request request = new Request.Builder()
                                .addHeader("Authorization","APPCODE"+MojiDataManager.AppCode)
                                .url(MojiDataManager.CONDITIONURL)
                                .post(requestBody)
                                .build();
                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                                String result = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {

                                        Gson gson = new Gson();
                                        Condition condition = gson.fromJson(result,Condition.class);

                                        String msg = "城市:"+condition.getDFGDF()+"\n";


                                        tvMainData.setText(msg);
                                        Log.i("TAG","run"+result);
                                    }
                                });
                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        BtnLaert = (Button) findViewById(R.id.BtnLaert);
        BtnApi = (Button) findViewById(R.id.BtnApi);
        Btn6days = (Button) findViewById(R.id.Btn6days);
        BtnCondition = (Button) findViewById(R.id.BtnCondition);
        BtnMainLimit = (Button) findViewById(R.id.BtnMainLimit);
        tvMainData = (TextView) findViewById(R.id.tvMainData);
    }
}